package com.feng.online_xdclass.controller;

import com.feng.online_xdclass.model.entity.User;
import com.feng.online_xdclass.model.request.LoginRequest;
import com.feng.online_xdclass.service.UserService;
import com.feng.online_xdclass.utils.JsonDate;

import com.feng.online_xdclass.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("api/v1/pri/user")
public class UserController {



    @Autowired
    private UserService userService;

    /**
     * 用户的注册接口
     * @param userInfo
     * @return
     */
    @PostMapping("register")
    public JsonDate register(@RequestBody Map<String,String> userInfo ){

        int rows = userService.save(userInfo);

        return rows == 1 ? JsonDate.bulidSuccess(): JsonDate.buildError("手机号已被注册！");

    }

    /**
     * 用户的登录接口
     * @param loginRequest
     * @return
     */
   @PostMapping("login")
    public JsonDate login(@RequestBody LoginRequest loginRequest){
      String token= userService.findByPhoneAndPwd(loginRequest.getPhone(),loginRequest.getPwd());
       System.out.println(loginRequest.getPwd());
      return token==null?JsonDate.buildError("登录失败,账号或密码错误"):JsonDate.bulidSuccess(token);

    }

    /**
     * 根据id查询用户信息
     * @return
     */
    @GetMapping("find_by_token")
    public JsonDate findUserInfoByToken(){
        User user = UserThreadLocal.get();
        Integer id = user.getId();
        if(id==null){
            return JsonDate.buildError("查询失败！");
        }
       User user1= userService.findByUserId(id);
        return  JsonDate.bulidSuccess(user1);

    }




}
